package com.zzy.furns.web;

import com.zzy.furns.domain.Member;
import com.zzy.furns.service.MemberService;
import com.zzy.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {

    private MemberService memberService = new MemberServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        if (memberService.isExistsUsername(username)){
            //用户名已存在，返回注册页面重新注册
            req.getRequestDispatcher("/views/member/login.jsp").forward(req,resp);
            return;
        }

        //用户名不存在
        if (!memberService.registerMember(new Member(null,username,password,email))){
            //注册失败
            req.getRequestDispatcher("/views/member/register_fail.html").forward(req,resp);
            return;
        }
        //注册成功
        req.getRequestDispatcher("/views/member/register_ok.html").forward(req,resp);
    }
}
